home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / utils / fmgr / which.c < prev   
Encoding:
C/C++ Source or Header  |  1993-04-11  |  2.1 KB  |  111 lines

  1. /* ----------------------------------------------------------------
  2.  *    which.c
  3.  *
  4.  *    $Header: /private/postgres/src/utils/fmgr/RCS/which.c,v 1.4 1990/02/12 19:50:02 cimarron Exp $
  5.  * ----------------------------------------------------------------
  6.  */
  7.  
  8. #include <sys/param.h>
  9. #include <sys/stat.h>
  10.  
  11.  
  12. /* This does the equivalent of /bin/which.  That is, given a filename,
  13. ** it searches directories named in the environment variable PATH, until
  14. ** it finds a file with that filename.
  15. */
  16. #ifdef linux
  17. #define copy_of(name) (strdup(name))
  18. #else
  19. #define copy_of(name) ((char*) sprintf(malloc(strlen(name)+1), "%s", name))
  20. #endif
  21.  
  22. static
  23. exists(filename)
  24.   char* filename;
  25. {
  26.   struct stat buffer;
  27.   return(stat(filename, &buffer) != -1);
  28.  
  29. }
  30.  
  31. /* The PATH string looks something like this:
  32. **
  33. ** .:/u/djones/bin:/usr/local/bin:/usr/mega/bin:/u/mega/bin
  34. **
  35. **
  36. ** except probably longer.
  37. */
  38.  
  39. char*
  40. which(file)
  41.   char* file;
  42. {
  43.  
  44.   if(*file == '/') return copy_of(file);
  45.  
  46.   {  char* search = (char*)getenv("PATH");
  47.  
  48.      if(search == 0)
  49.        search = ".:~/bin:/usr/mega/bin:/usr/local/bin:/usr/new:/usr/ucb:/usr/bin:/bin:/usr/hosts:/usr/games";
  50.  
  51.      { register char* ptr = search;
  52.        
  53.        while(*ptr)
  54.      { char  name[MAXPATHLEN];
  55.        register char* next = name;
  56.         
  57.        /* copy directory name into [name] */
  58.        while(*ptr && *ptr != ':') *next++ = *ptr++;
  59.        if(*ptr) ptr++;
  60.        
  61.        *next++ = '/'; /* separates directory name and filename */
  62.        
  63.        /* copy filename into [name] */
  64.        { register char* ptr2 = file;
  65.          while(*ptr2) *next++ = *ptr2++;
  66.        }
  67.        
  68.        *next = '\0';
  69.        
  70.        { char* afile = (char*)(name);
  71.          if(exists(afile)) return (afile);
  72.          free(afile);
  73.        }
  74.        
  75.      }
  76.        
  77.      }
  78.      return file;
  79.    }
  80. }
  81.  
  82. #undef binwhich
  83. #ifdef binwhich
  84. main(argc, argv)
  85.   char** argv;
  86. {
  87.  
  88.   argc--; argv++;
  89.  
  90.   while (argc)
  91.     { char* path = which(*argv);
  92.       if(path)
  93.     {
  94.       printf("%s\n", path);
  95.       free(path);
  96.     }
  97.       else
  98.     { char* ptr = search;
  99.       printf("no %s in ", *argv);
  100.       while(*ptr)
  101.         { putchar(*ptr==':' ? ' ' : *ptr);
  102.           ptr++;
  103.         }
  104.       putchar('\n');
  105.     }
  106.       argc--; argv++; 
  107.     }
  108.   exitpg(0);
  109. }
  110. #endif binwhich
  111.